//
//  KKAlertView.m
//  Mice
//
//  Created by mac on 14/10/31.
//  Copyright (c) 2014年 Leo. All rights reserved.
//

#import "KKAlertView.h"

@interface KKAlertView() <UIAlertViewDelegate> {
    
    KKAlertViewCancelBolck _cancelHandle;
    KKAlertViewSureBolck _sureHandle;
    
    UIAlertView* _alertView;
    UIAlertController* _alertController;
}

@end

@implementation KKAlertView

+ (instancetype)shareInterce {
    
    static KKAlertView* alertView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alertView = [self new];
    });
    alertView.customMessageColor = nil;
    return alertView;
}

- (void)showWithTitle:(NSString*)title andWithMessage:(NSString*)message andWithCancelTitle:(NSString*)cancel andWithCancelHandle:(KKAlertViewCancelBolck)cancelHandle andWithSureTitle:(NSString*)sure andWithSureHandle:(KKAlertViewSureBolck)sureHandle {
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        
        if (_alertView) {
            return;
        }
        
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle: title message: message delegate: self cancelButtonTitle: cancel otherButtonTitles: sure, nil];
        _alertView = alertView;
        _cancelHandle = cancelHandle;
        _sureHandle = sureHandle;
        _alertController = nil;
        
        [_alertView show];
    }
    else {
        
        if (_alertController) {
            return;
        }
        
        UIAlertController* alertController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleAlert];
        
        if (_customMessageColor) {
            
            NSMutableAttributedString *messageString = [[NSMutableAttributedString alloc] initWithString: message];
            [messageString addAttribute:NSForegroundColorAttributeName value: _customMessageColor range:NSMakeRange(0,messageString.length)];
            
            [alertController setValue:messageString forKey:@"attributedMessage"];
            _customMessageColor = nil;
        }
        
        if (cancel) {
            [alertController addAction: [UIAlertAction actionWithTitle: cancel style: UIAlertActionStyleDefault handler: ^(UIAlertAction* action){
                
                _alertController = nil;
                if (cancelHandle) {
                    cancelHandle();
                }
            }]];
        }
        if (sure) {
            
            [alertController addAction: [UIAlertAction actionWithTitle: sure style: UIAlertActionStyleDefault handler: ^(UIAlertAction* action){
                _alertController = nil;
                if (sureHandle) {
                    sureHandle();
                }
            }]];
        }

        _alertController = alertController;
        _alertView = nil;
        
        [[[[UIApplication sharedApplication].delegate window] rootViewController] presentViewController: _alertController animated: YES completion: nil];
    }
}

#pragma mark - UIAlertViewDelegate methods

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView == _alertView) {
        
        if (buttonIndex == _alertView.cancelButtonIndex) {
            if (_cancelHandle) {
                _cancelHandle();
            }
        }
        else if (buttonIndex == _alertView.firstOtherButtonIndex) {
            if (_sureHandle) {
                _sureHandle();
            }
        }
        _alertView = nil;
    }
}

- (void)alertViewCancel:(UIAlertView *)alertView {
    
    if (alertView == _alertView) {
        _alertView = nil;
        if (_cancelHandle) {
            _cancelHandle();
        }
    }
}

@end


#pragma mark -
@interface KKResultAlertView() {
    
    UIImageView* _imageView;
    UILabel* _messageLabel;
    
    KKResultAlertViewBlock _block;
    BOOL _isAppear;
}

@end

@implementation KKResultAlertView

static KKResultAlertView* alertView = nil;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alertView = [KKResultAlertView new];
    });
    return alertView;
}

- (id)init {
    self = [super initWithFrame: CGRectMake(0, 0, 100, 100)];
    if (self) {
        
        self.layer.cornerRadius = 10.0f;
        self.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.6];
        
        _imageView = [[UIImageView alloc] initWithFrame: CGRectMake(30, 20, 40, 40)];
        _imageView.image = [UIImage imageNamed: @"alertView_successful"];
        [self addSubview: _imageView];
        
        _messageLabel = [[UILabel alloc] initWithFrame: CGRectMake(3, 70, CGRectGetWidth(self.bounds) - 6, 20)];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.backgroundColor = [UIColor clearColor];
        _messageLabel.textColor = [UIColor whiteColor];
        _messageLabel.font = [UIFont systemFontOfSize: 14];
        _messageLabel.adjustsFontSizeToFitWidth = YES;
        [self addSubview: _messageLabel];
    }
    return self;
}

- (void)alertViewShowMessage:(NSString*)message andWithSuccessful:(BOOL)successful andWithCallBack:(KKResultAlertViewBlock)block {
    
    if (message.length == 0 || _isAppear) {
        return;
    }
    _block = block;
    _isAppear = YES;
    
    _imageView.image = [UIImage imageNamed: successful ? @"alertView_successful" : @"alertView_faile"];
    
    _messageLabel.text = message;
    
    UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
    self.center = CGPointMake(CGRectGetWidth(keyWindow.bounds)/2, CGRectGetHeight(keyWindow.bounds)/2);
    [keyWindow addSubview: self];
    
    __weak KKResultAlertView* weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf alertViewDidDisappear];
    });
}

- (void)alertViewDidDisappear {
    
    [self removeFromSuperview];
    if (_block) {
        _block();
    }
    _isAppear = NO;
}

@end
